package com.sunteam.flutter_oss_plugin

import android.content.Context
import android.util.Log
import com.alibaba.sdk.android.oss.*
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback
import com.alibaba.sdk.android.oss.common.OSSLog
import com.alibaba.sdk.android.oss.common.auth.OSSAuthCredentialsProvider
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask
import com.alibaba.sdk.android.oss.model.PutObjectRequest
import com.alibaba.sdk.android.oss.model.PutObjectResult

class OssManager {
    var oss: OSS? = null

    fun initOss(
        endPoint: String,
        stsServer: String,
        userAgent: String,
        needLog: Boolean,
        needDns: Boolean,
        connectTimeOut: Int,
        socketTimeOut: Int,
        maxConcurrentNumber: Int,
        maxRetryNumber: Int,
        ctx: Context
    ) {
        var conf = ConfigManager.buildConfig(
            userAgent, needLog, needDns, connectTimeOut,
            socketTimeOut, maxConcurrentNumber, maxRetryNumber
        )
        val credentialProvider: OSSCredentialProvider = OSSAuthCredentialsProvider(stsServer)
        oss = OSSClient(ctx, endPoint, credentialProvider, conf)

        Log.e("OssManager", "initComplete")
    }

    fun uploadFileByPath(
        bucketName: String,
        objectName: String,
        filePath: String,
        resultCallBack: OSSCompletedCallback<PutObjectRequest, PutObjectResult>,
        progressCallback: OSSProgressCallback<PutObjectRequest>
    ): OSSAsyncTask<PutObjectResult>? {
        return FileManager.uploadFileByPath(
            oss,
            bucketName,
            objectName,
            filePath,
            resultCallBack,
            progressCallback
        )
    }

    fun getFileOssPath(bucketName: String, objectName: String,isSign:Boolean): String {
        return FileManager.getFileOssPath(oss, bucketName, objectName,isSign)
    }

}